import qs from 'qs';

// const HOST = 'http://192.168.0.106:8080/'
// const HOST = 'http://192.168.0.100:8008/'
const HOST = 'http://hzmapi.qiankaiwangluo.com:8080'

// 对数据请求的封装
export function get (url,data={}) {
  return request(url,"GET",data)
}

export function post (url,data={}) {
	data = qs.stringify(data)
  return request(url,"POST",data)
}

function request(url, method, data) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: HOST + url,
			method,
			data,
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			success: res => {
				switch (res.data.code){
					case '1':
						resolve(res.data.data)
						break;
					default:
						uni.showModal({
							title: '失败',
							content: res.data.msg,
							success: function (res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
						break;
				}
			},
			fail: rej => {
				reject(rej)
			}
		})
	})
}
